package com.yanqu.road.server.gameplayer.module.activity.fivehero;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.fivehero.FiveHeroConfig;
import com.yanqu.road.entity.activity.fivehero.FiveHeroUserData;
import com.yanqu.road.entity.activity.fivehero.config.*;
import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.entity.config.skill.SkillInfo;
import com.yanqu.road.entity.config.skin.SkinInfo;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.eShopType;
import com.yanqu.road.entity.enums.eSkillType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.entity.player.UserPatronsSkill;
import com.yanqu.road.logic.bussiness.activity.FiveHeroBusiness;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.FiveHeroProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PlayerModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;

import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.VipMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.activity.curio.CurioMgr;
import com.yanqu.road.server.manger.activity.fivehero.FiveHeroMgr;
import com.yanqu.road.server.manger.config.SkillMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.*;

public class FiveHeroModule extends GeneralModule {


    private FiveHeroUserData userData;

    public FiveHeroModule(GamePlayer player) {
        super(player);
    }

    public FiveHeroUserData getUserData(){
        return userData;
    }



    @Override
    public boolean loadData() {
        if(SystemOpenMgr.systemOpen(player, eSystemId.FiveHeroActivity.getValue())){
            ActivityInfo activityInfo = FiveHeroMgr.getActivityInfo();
            if(activityInfo != null){
                userData = FiveHeroBusiness.getUserData(getUserId(),activityInfo.getActivityId());
                if(userData == null){
                    initUserData(activityInfo.getActivityId());
                }
            }
        }
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return false;
    }

    @Override
    public boolean saveData() {
        if(userData != null){
            if(userData.isInsertOption()){
                FiveHeroBusiness.addUserData(userData);
            }else if(userData.isUpdateOption()){
                FiveHeroBusiness.updateUserData(userData);
            }
        }
        return true;
    }

    @Override
    public void afterLogin() {
        if(SystemOpenMgr.systemOpen(player, eSystemId.FiveHeroActivity.getValue())){
            ActivityInfo activityInfo = FiveHeroMgr.getActivityInfo();
            if(ActivityHelper.activityInShowTime(activityInfo)){
                syncConfigData();
                syncUserData(true);
            }
        }
    }

    public void syncUserData(boolean syncPatrons){
        ActivityInfo activityInfo = FiveHeroMgr.getActivityInfo();
        if(ActivityHelper.activityInShowTime(activityInfo)){
            //跨服的自己的数据同步
            FiveHeroProto.CrossFiveHeroLoginReqMsg.Builder msg = FiveHeroProto.CrossFiveHeroLoginReqMsg.newBuilder();
            msg.setActivityId(activityInfo.getActivityId());
            UserBaseInfo userBaseInfo = UserMgr.getUserBaseInfo(player.getUserId(), GameServer.getInstance().getServerId());
            if(userBaseInfo != null){
                msg.setUserBaseInfo(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
                msg.setLanguage(player.getLanguage());
            }
            msg.setPopularity(userData.getIntegral());
            if(syncPatrons){
                //藏品加成
                int curioAddition = (int)CurioMgr.getSkillAdditionBySkillType(eSkillType.FiveHeroAttackSkill.getValue(), player, 0);
                List<UserPatrons> patronsList = player.getModule(PatronsModule.class).getUserPatronsList();
                for(UserPatrons userPatrons : patronsList){
                    FiveHeroProto.FiveHeroPatronsTempMsg.Builder patronsMsg = FiveHeroProto.FiveHeroPatronsTempMsg.newBuilder();
                    patronsMsg.setId(userPatrons.getPatronsId());
                    patronsMsg.setSkin(userPatrons.getSkinId());
                    patronsMsg.setAbility(userPatrons.getAbility().longValue());
                    patronsMsg.setAddPer(FiveHeroMgr.getSkillAddPer(userPatrons));
                    msg.addPatrons(patronsMsg);
                }
                msg.setCurioAddition(curioAddition);
            }
//            YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.C2_CROSS_FIVE_HERO_LOGIN, msg);
//            GamePlayerMgr.sendChannelPacket(player.getUserId(), pbMsg);
            int groupId = GameUnionActivityGroupMgr.getGroupId(activityInfo.getActivityId(),userData.getUserId());
            GamePlayerMgr.sendUnionActivityGroupPacket(CrossProtocol.C2_CROSS_FIVE_HERO_LOGIN, msg,player.getUserId(),activityInfo.getActivityId(),groupId);
        }
    }


    public void initUserData(int activityId){
        if(userData == null || userData.getActivityId() != activityId){
            FiveHeroUserData tempUserData = new FiveHeroUserData();
            tempUserData.setActivityId(activityId);
            tempUserData.setUserId(getUserId());
            tempUserData.setIntegral(0);
            tempUserData.setInsertOption();
            userData = tempUserData;
        }
    }

    /**
     * 系统解锁
     */
    public void initSystem() {
        ActivityInfo activityInfo = FiveHeroMgr.getActivityInfo();
        if(ActivityHelper.activityInShowTime(activityInfo)){
            initUserData(activityInfo.getActivityId());
            syncConfigData();
            syncUserData(true);
        }
    }

    public void pvpHpAdd(long hpNum){
        if(FiveHeroMgr.getActivityInfo() != null){
            FiveHeroProto.FiveHeroHpAddMsg.Builder reqMsg = FiveHeroProto.FiveHeroHpAddMsg.newBuilder();
            reqMsg.setActivityId(FiveHeroMgr.getActivityInfo().getActivityId());
            reqMsg.setUserId(getUserId());
            reqMsg.setAddValue((int)hpNum);
//            player.sendChannelPacket(Protocol.C2_CROSS_FIVE_HERO_PVP_HP_ADD, reqMsg);
            int groupId = GameUnionActivityGroupMgr.getGroupId(reqMsg.getActivityId(),player.getUserId());
            GamePlayerMgr.sendUnionActivityGroupPacket(CrossProtocol.C2_CROSS_FIVE_HERO_PVP_HP_ADD,
                    reqMsg,player.getUserId(),reqMsg.getActivityId(),groupId);
        }
    }

    //残卷加进度条
    public void bookValueAdd(long addValue,long count){
        if(FiveHeroMgr.getActivityInfo() != null){
            FiveHeroProto.CrossFiveHeroBookAddMsg.Builder reqMsg = FiveHeroProto.CrossFiveHeroBookAddMsg.newBuilder();
            reqMsg.setActivityId(FiveHeroMgr.getActivityInfo().getActivityId());
            reqMsg.setUserId(getUserId());
            reqMsg.setAddValue((int)addValue);
            reqMsg.setTimes((int)count);
//            player.sendChannelPacket(Protocol.C2_CROSS_FIVE_HERO_BOOK_ADD, reqMsg);
            int groupId = GameUnionActivityGroupMgr.getGroupId(reqMsg.getActivityId(),player.getUserId());
            GamePlayerMgr.sendUnionActivityGroupPacket(CrossProtocol.C2_CROSS_FIVE_HERO_BOOK_ADD,
                    reqMsg,player.getUserId(),reqMsg.getActivityId(),groupId);
        }
    }

    public void syncConfigData(){
        String language = player.getUserInfo().getLanguage();
        FiveHeroConfig config = FiveHeroMgr.getConfig();
        Map<Integer, FiveHeroTypeConfig> typeConfigMap = config.getTypeConfigMap();
        Map<Integer, FiveHeroCityConfig> cityConfigMap = config.getCityConfigMap();
        Map<Integer, FiveHeroBuffConfig> buffConfigMap = config.getBuffConfigMap();
        Map<Integer, FiveHeroEventConfig> eventConfigMap = config.getEventConfigMap();
        Map<Integer, FiveHeroBookConfig> bookConfigMap = config.getBookConfigMap();
        Map<Integer, FiveHeroBanditConfig> banditConfigMap = config.getBanditConfigMap();
        Map<Integer, FiveHeroTechConfig> techConfigMap = config.getTechConfigMap();
        Map<Integer, FiveHeroOrderConfig> orderConfigMap = config.getOrderConfigMap();
        Map<Integer, FiveHeroTitleConfig> titleConfigMap = config.getTitleConfigMap();
        FiveHeroProto.FiveHeroSyncConfigMsg.Builder configMsg = FiveHeroProto.FiveHeroSyncConfigMsg.newBuilder();
        for(FiveHeroTypeConfig typeConfig : typeConfigMap.values()){
            FiveHeroProto.FiveHeroTypeConfigMsg.Builder typeConfigMsg = FiveHeroProto.FiveHeroTypeConfigMsg.newBuilder();
            typeConfigMsg.setType(typeConfig.getType());
            typeConfigMsg.setIcon(typeConfig.getIcon() + "");
            typeConfigMsg.setName(ServerLanguageMgr.getContent(typeConfig.getName(), language));
            typeConfigMsg.setCampPoint(typeConfig.getCampPoint());
            typeConfigMsg.setCampScore(typeConfig.getCampScore());
            typeConfigMsg.setTeamNum(typeConfig.getTeamNum());
            typeConfigMsg.setPvpTeamDrawWeights(typeConfig.getPvpTeamDrawWeights());
            typeConfigMsg.setPveTeamDrawWeights(typeConfig.getPveTeamDrawWeights());
            typeConfigMsg.setAddition(typeConfig.getAddition());
            typeConfigMsg.setEnergyCost(typeConfig.getEnergyCost());
            typeConfigMsg.setEventList(typeConfig.getEventList());
            typeConfigMsg.setDiffPoolFightOrder(typeConfig.getDiffPoolFightOrder());
            typeConfigMsg.setBlood(typeConfig.getBlood());
            configMsg.addTypeConfig(typeConfigMsg);
        }
        for(FiveHeroCityConfig cityConfig : cityConfigMap.values()){
            FiveHeroProto.FiveHeroCityConfigMsg.Builder cityConfigMsg = FiveHeroProto.FiveHeroCityConfigMsg.newBuilder();
            cityConfigMsg.setCityId(cityConfig.getId());
            cityConfigMsg.setName(ServerLanguageMgr.getContent(cityConfig.getName(), language));
            cityConfigMsg.setType(cityConfig.getType());
            cityConfigMsg.setInitialCamp(cityConfig.getInitialCamp());
            cityConfigMsg.setShowType(cityConfig.getShowType());
            cityConfigMsg.setJobBuffEffect(cityConfig.getJobBuffEffect());
            configMsg.addCityConfig(cityConfigMsg);
        }
        for(FiveHeroBuffConfig buffConfig : buffConfigMap.values()){
            FiveHeroProto.FiveHeroBuffTypeConfigMsg.Builder buffConfigMsg = FiveHeroProto.FiveHeroBuffTypeConfigMsg.newBuilder();
            buffConfigMsg.setId(buffConfig.getId());
            buffConfigMsg.setType(buffConfig.getType());
            buffConfigMsg.setIcon(buffConfig.getIcon());
            buffConfigMsg.setName(ServerLanguageMgr.getContent(buffConfig.getName(), language));
            buffConfigMsg.setDesc(ServerLanguageMgr.getContent(buffConfig.getDesc(), language));
            buffConfigMsg.setBuffEffect(buffConfig.getBuffEffect());
            buffConfigMsg.setContinueTime(buffConfig.getContinueTime());

            configMsg.addBuffTypeConfig(buffConfigMsg);
        }


        for(FiveHeroEventConfig eventConfig : eventConfigMap.values()){
            FiveHeroProto.FiveHeroEventConfigMsg.Builder eventConfigMsg = FiveHeroProto.FiveHeroEventConfigMsg.newBuilder();
            eventConfigMsg.setId(eventConfig.getId());
            eventConfigMsg.setType(eventConfig.getType());
            eventConfigMsg.setParameter(eventConfig.getParameter());
            eventConfigMsg.setWeight(eventConfig.getWeight());
            eventConfigMsg.setIcon(eventConfig.getIcon() + "");
            eventConfigMsg.setRewards(eventConfig.getRewards());
            eventConfigMsg.setDesc(ServerLanguageMgr.getContent(eventConfig.getDesc(), language));
            eventConfigMsg.setKillRewards(eventConfig.getKillRewards());
            eventConfigMsg.setWinRewards(eventConfig.getWinRewards());
            eventConfigMsg.setBloodLossRewards(eventConfig.getBloodLossRewards());
            eventConfigMsg.setDes(ServerLanguageMgr.getContent(eventConfig.getDes(), language));
            eventConfigMsg.setQuality(eventConfig.getQuality());
            eventConfigMsg.setFailRewards(eventConfig.getFailRewards());
            configMsg.addEventConfig(eventConfigMsg);
        }

        for(FiveHeroBookConfig bookConfig : bookConfigMap.values()){
            FiveHeroProto.FiveHeroBookConfigMsg.Builder bookConfigMsg = FiveHeroProto.FiveHeroBookConfigMsg.newBuilder();
            bookConfigMsg.setId(bookConfig.getId());
            bookConfigMsg.setName(ServerLanguageMgr.getContent(bookConfig.getName(), language));
            bookConfigMsg.setDesc(ServerLanguageMgr.getContent(bookConfig.getDesc(), language));
            bookConfigMsg.setQuality(bookConfig.getQuality());
            bookConfigMsg.setSchedule(bookConfig.getSchedule());
            bookConfigMsg.setWeights(bookConfig.getWeights());
            bookConfigMsg.setShowWeights(bookConfig.getShowWeights());
            bookConfigMsg.setReward(bookConfig.getReward());
            bookConfigMsg.setScheduleEffect(bookConfig.getScheduleEffect());
            bookConfigMsg.setShowPos(bookConfig.getShowPos());
            bookConfigMsg.setShowScale(bookConfig.getShowScale());
            configMsg.addBookConfig(bookConfigMsg);
        }

        for(FiveHeroBanditConfig banditConfig : banditConfigMap.values()){
            FiveHeroProto.FiveHerBanditConfigMsg.Builder banditConfigMsg = FiveHeroProto.FiveHerBanditConfigMsg.newBuilder();
            banditConfigMsg.setId(banditConfig.getId());
            banditConfigMsg.setName(ServerLanguageMgr.getContent(banditConfig.getName(), language));
            banditConfigMsg.setIcon(banditConfig.getIcon() + "");
            configMsg.addBanditConfig(banditConfigMsg);
        }

        for(FiveHeroTechConfig techConfig : techConfigMap.values()){
            FiveHeroProto.FiveHerTechConfigMsg.Builder techConfigMsg = FiveHeroProto.FiveHerTechConfigMsg.newBuilder();
            techConfigMsg.setLevel(techConfig.getLevel());
            techConfigMsg.setAdd(techConfig.getAdd());
            techConfigMsg.setCost(techConfig.getCost());
            techConfigMsg.setPerthAdd(techConfig.getPerthAdd());
            configMsg.addTechConfig(techConfigMsg);
        }

        for(FiveHeroOrderConfig orderConfig : orderConfigMap.values()){
            FiveHeroProto.FiveHerOrderConfigMsg.Builder orderConfigMsg = FiveHeroProto.FiveHerOrderConfigMsg.newBuilder();
            orderConfigMsg.setId(orderConfig.getId());
            orderConfigMsg.setName(ServerLanguageMgr.getContent(orderConfig.getName(), language));
            orderConfigMsg.setRecoverNum(orderConfig.getRecoverNum());
            orderConfigMsg.setTeamName(orderConfig.getTeamName());
            configMsg.addOrderConfig(orderConfigMsg);
        }

        for(FiveHeroTitleConfig titleConfig : titleConfigMap.values()){
            FiveHeroProto.FiveHeroTitleConfigMsg.Builder titleConfigMsg = FiveHeroProto.FiveHeroTitleConfigMsg.newBuilder();
            titleConfigMsg.setId(titleConfig.getId());
            titleConfigMsg.setCampId(titleConfig.getCampId());
            titleConfigMsg.setIcon(titleConfig.getIcon());
            titleConfigMsg.setName(ServerLanguageMgr.getContent(titleConfig.getName(), language));
            titleConfigMsg.setRewards(titleConfig.getRewards());
            configMsg.addTitleConfig(titleConfigMsg);
        }

        player.sendPacket(Protocol.U_FIVEHERO_CONFIG_SYNC, configMsg);
    }


    public FiveHeroProto.CrossFiveHeroUpdatePatronsUserMsg.Builder getSyncPatronsMsg() {
        FiveHeroConfig config = FiveHeroMgr.getConfig();
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            return null;
        }
        FiveHeroProto.CrossFiveHeroUpdatePatronsUserMsg.Builder builder = FiveHeroProto.CrossFiveHeroUpdatePatronsUserMsg.newBuilder();
        builder.setUserId(player.getUserId());
        List<UserPatrons> patronsList = player.getModule(PatronsModule.class).getUserPatronsList();
        //藏品加成
        int curioAddition = (int)CurioMgr.getSkillAdditionBySkillType(eSkillType.FiveHeroAttackSkill.getValue(), player, 0);
        for(UserPatrons userPatrons : patronsList){
            FiveHeroProto.FiveHeroPatronsTempMsg.Builder patronsMsg = FiveHeroProto.FiveHeroPatronsTempMsg.newBuilder();
            patronsMsg.setId(userPatrons.getPatronsId());
            patronsMsg.setSkin(userPatrons.getSkinId());
            patronsMsg.setAbility(userPatrons.getAbility().longValue());
            patronsMsg.setAddPer(FiveHeroMgr.getSkillAddPer(userPatrons));
            builder.addPatrons(patronsMsg);
        }
        builder.setCurioAddition(curioAddition);
        return builder;
    }

    public void syncShopData() {
        ActivityInfo activityInfo = FiveHeroMgr.getActivityInfo();
        if (!ActivityMgr.activityInShowTime(activityInfo)) {
            return;
        }

        if (userData == null) {
            log.error("nmb data is null. {}", activityInfo.getActivityId());
            return;
        }
        FiveHeroProto.FiveHeroShopDataMsg.Builder builder = FiveHeroProto.FiveHeroShopDataMsg.newBuilder();
        builder.setCurrentMallId(userData.getCurrentMall());
        for (Map.Entry<Integer, Long> entry : userData.getMallMap().entrySet()) {
            int mallId = entry.getKey();
            long mallTime = entry.getValue();
            int mallTimes = userData.getMallTimes(mallId);

            if (mallTimes <= 0) {
                continue;
            }

            FiveHeroProto.FiveHeroMallEntity.Builder msg = FiveHeroProto.FiveHeroMallEntity.newBuilder();
            msg.setMallId(mallId);
            msg.setTime(mallTime);
            msg.setCanBuyTimes(mallTimes);
            builder.addMallInfo(msg);
        }
        player.sendPacket(ClientProtocol.U_FIVEHERO_GET_SHOP_DATA, builder);
    }


    /**
     * 商城购买
     * @param mallId
     */
    public void shopBuy(int mallId) {
        try {
            ActivityInfo activityInfo = FiveHeroMgr.getActivityInfo();
            if (!ActivityMgr.activityInShowTime(activityInfo)) {
                return;
            }

            if (userData == null) {
                return;
            }

            if (userData.getMallTimes(mallId) > 0) {
                // 优先扣背包的
                userData.reduceMallTimes(mallId, 1);
                if (userData.getMallTimes(mallId) <= 0) {
                    userData.removeMall(mallId);
                }
            } else if (mallId == userData.getCurrentMall()) {
                userData.setCurrentMall(0);
            }

            syncShopData();
        } catch (Exception ex) {
            log.error(ex);
        } finally {

        }

    }


    /**
     * 是否可以到账成功 不判断时间 只要有就可以到账
     * @param mallId
     * @return
     */
    public boolean canChargeSucceed(int mallId) {
        ActivityInfo activityInfo = FiveHeroMgr.getActivityInfo();
        if (!ActivityMgr.activityInShowTime(activityInfo)) {
            return false;
        }

        if (userData == null) {
            return false;
        }

        if (userData.getCurrentMall() == mallId || userData.getMallMap().containsKey(mallId)) {
            return true;
        }

        return false;
    }
    /**
     * 是否可下单
     * @param mallId
     * @return
     */
    public boolean canPrecharge(int mallId) {
        ActivityInfo activityInfo = FiveHeroMgr.getActivityInfo();
        if (!ActivityMgr.activityInShowTime(activityInfo)) {
            return false;
        }

        if (userData == null) {
            return false;
        }

        if (userData.getCurrentMall() == mallId) {
            return true;
        }

        if (System.currentTimeMillis() <= (userData.getMallTime(mallId) + FiveHeroMgr.getConfig().getRmbMallTime())) {
            return true;
        }
        return false;
    }

    /**
     * 通知商城
     */
    public int notifyShopEvent() {
        ActivityInfo activityInfo = FiveHeroMgr.getActivityInfo();
        if (!ActivityMgr.activityInShowTime(activityInfo)) {
            return 0;
        }

        if (userData == null) {
            return 0;
        }

        List<MallInfo> list = getMallList(activityInfo.getActivityId());
        if (list.size() > 0) {
            int triggerMallId = getVipLevelMallId(list);
            if(triggerMallId > 0) {
                long lastMallTime = userData.getMallTime(triggerMallId);
                long nowTime = System.currentTimeMillis();
                if (nowTime > (lastMallTime + FiveHeroMgr.getConfig().getRmbMallTime())) {
                    userData.clearMallTimes(triggerMallId);
                }
            }
            userData.setCurrentMall(triggerMallId);
            return triggerMallId;
        }
//        syncShopData();
        return 0;
    }


    /**
     * 跳过商店事件
     */
    public void giveUpShopEvent() {
        ActivityInfo activityInfo = FiveHeroMgr.getActivityInfo();
        if (!ActivityMgr.activityInShowTime(activityInfo)) {
            return;
        }

        if (userData == null) {
            return;
        }

        if (userData.getCurrentMall() > 0) {
            // 未购买的需要处理一下 放进未购买列表
            long lastMallTime = userData.getMallTime(userData.getCurrentMall());
            long nowTime = System.currentTimeMillis();
            if (nowTime > (lastMallTime + FiveHeroMgr.getConfig().getRmbMallTime())) {
                userData.clearMallTimes(userData.getCurrentMall());
            }
            userData.setMallTime(userData.getCurrentMall(), nowTime);
            userData.addMallTimes(userData.getCurrentMall(), 1);

            // 重置当前的商品id为0//不用设置了，和原先的不一样了
//            userData.setCurrentMall(0);
        }
        syncShopData();
    }

    /**
     * 获取商城列表
     * @param activityId
     * @return
     */
    public List<MallInfo> getMallList(int activityId) {
        Map<Integer, MallInfo> mallInfoMap = NormalActivityMgr.getMallInfoTypeMap(activityId, eShopType.FiveHeroMoneyMall.getValue());
        List<MallInfo> list = new ArrayList<>(mallInfoMap.values());
        list.sort(new Comparator<MallInfo>() {
            @Override
            public int compare(MallInfo o1, MallInfo o2) {
                int l2 = Integer.valueOf(StringUtils.stringToIntegerList(o2.getExtendParam(), "\\|").get(1));
                int l1 = Integer.valueOf(StringUtils.stringToIntegerList(o1.getExtendParam(), "\\|").get(1));
                if (l2 == l1) {
                    return 0;
                } else if (l2 > l1) {
                    return 1;
                } else {
                    return -1;
                }
            }
        });
        return list;
    }

    /**
     * 获取VIP等级对应的mallId
     * @param list
     * @return
     */
    public int getVipLevelMallId(List<MallInfo> list) {
        int vipLevel = VipMgr.getVipLv(player.getModule(PlayerModule.class).getUserInfo().getVipExp());

        Set<Integer> typeSet = new HashSet<>();
        for (MallInfo mallInfo : list) {
            List<Integer> paramList = StringUtils.stringToIntegerList(mallInfo.getExtendParam(), "\\|");
            typeSet.add(paramList.get(0));
        }

        int mallId = 0;
        if (typeSet.size() > 0) {
            // 随机一个类型
            List<Integer> typeList = new ArrayList<>(typeSet);
            int type = typeList.get(new Random().nextInt(typeList.size()));


            for (MallInfo mallInfo : list) {
                List<Integer> paramList = StringUtils.stringToIntegerList(mallInfo.getExtendParam(), "\\|");

                if (type == paramList.get(0) && vipLevel >= paramList.get(1)) {
                    mallId = mallInfo.getMallId();
                    break;
                }
            }
        }
        return mallId;
    }

    public void addPopularity(int goodsId,BigInteger countByGoodsId) {
        userData.setIntegral(userData.getIntegral() + countByGoodsId.longValue());
        syncUserData(false);
        LogMgr.addLogGoods(goodsId, player.getUserId(), true, countByGoodsId.longValue(), userData.getIntegral(), eLogMoneyType.FiveHeroPopularityReward, player.getUserInfo().getVipExp());
    }

    public void getFiveHeroBookReward(FiveHeroProto.CrossFiveHeroBookRewardNoticeMsg reqMsg) {
        FiveHeroConfig config = FiveHeroMgr.getConfig();
        if(config == null){
            log.error("书本奖励领取异常{}",getUserId());
            log.error("书本奖励领取异常{}个",reqMsg.getBookList().size());
            return ;
        }
        Property allProp = new Property();
        for(FiveHeroProto.FiveHeroBookTempMsg bookMsg : reqMsg.getBookList()){
            FiveHeroBookConfig bookConfig = config.getBookConfig(bookMsg.getBookId());
            Property reward = PropertyHelper.parseNewStringToProperty(bookConfig.getReward());
            player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.FiveHero,eLogMoneyType.FiveHeroGetBookReward);
            allProp.addProperty(reward);
        }
        FiveHeroProto.FiveHeroGetBookRewardRespMsg.Builder respMsg = FiveHeroProto.FiveHeroGetBookRewardRespMsg.newBuilder();
        respMsg.setRet(0);
        respMsg.setReward(PropertyHelper.parsePropertyToString(allProp));
        player.sendPacket(ClientProtocol.U_FIVEHERO_GET_BOOK_REWARD,respMsg);
    }

    public void getWinnerInfo(FiveHeroProto.FiveHeroGetWinnerRespMsg respMsg) {
        FiveHeroProto.FiveHeroGetWinnerRespMsg.Builder builder = respMsg.toBuilder();
        if(userData != null){
            Map<Integer, Long> worshipMap = userData.getWorshipTimeMap();
            for(FiveHeroProto.FiveHeroWinnerMsg winnerMsg : respMsg.getWinnerList()){
                builder.addWinner(winnerMsg.toBuilder().setWorshipTime((int)(worshipMap.getOrDefault(winnerMsg.getCampId(),0L)/1000)));
            }
        }
        player.sendPacket(ClientProtocol.U_FIVEHERO_GET_WINNER_INFO,builder);
    }

    public int worship(int campId) {
        FiveHeroProto.FiveHeroCityWinnerWorshipRespMsg.Builder respMsg = FiveHeroProto.FiveHeroCityWinnerWorshipRespMsg.newBuilder();
        respMsg.setRet(0);
        if(userData != null){
            for(Long time : userData.getWorshipTimeMap().values()){
                long lastZeroTime = DateHelper.getZeroTimeStamp(time);
                long todayZeroTime = DateHelper.getTodayZeroTimeStamp();
                if(todayZeroTime == lastZeroTime){
                    return GameErrorCode.E_FIVE_HERO_HAS_WORSHIP;
                }
            }
            Map<Integer, Long> worshipMap = userData.getWorshipTimeMap();
            long lastTime = worshipMap.getOrDefault(campId,0L);
            long lastZeroTime = DateHelper.getZeroTimeStamp(lastTime);
            long todayZeroTime = DateHelper.getTodayZeroTimeStamp();
            if(todayZeroTime == lastZeroTime){
                return GameErrorCode.E_FIVE_HERO_HAS_WORSHIP;
            }
            String rewardStr = FiveHeroMgr.getWorshipReward(campId);
            player.getModule(CurrencyModule.class).addCurrency(rewardStr,eLogMoneyType.FiveHero,eLogMoneyType.FiveHeroGetWorshipReward);
            respMsg.setReward(rewardStr);
            worshipMap.put(campId,System.currentTimeMillis());
            userData.setWorshipTimeMap(worshipMap);
        }
        player.sendPacket(ClientProtocol.U_FIVEHERO_WORSHIP,respMsg);
        return 0;
    }

    //复活门客预扣除
    public void fuhuo(FiveHeroProto.FiveHeroRevivePatronsReqMsg reqMsg) {
        FiveHeroProto.FiveHeroRevivePatronsRespMsg.Builder respMsg = FiveHeroProto.FiveHeroRevivePatronsRespMsg.newBuilder();
        respMsg.setRet(0);
        if(reqMsg.getType() != 4 && reqMsg.getType() != 1){
            respMsg.setRet(GameErrorCode.E_FIVE_HERO_PARAMS_ERR);
            player.sendPacket(ClientProtocol.U_FIVEHERO_REVIVE_PATRONS,respMsg);
            return;
        }
        FiveHeroConfig config = FiveHeroMgr.getConfig();
        String cost = "";
        if(reqMsg.getType() == 1){
            cost = "";
        }else if(reqMsg.getType() == 4){
            cost = "";
        }

        if(player.getModule(CurrencyModule.class).currencyIsEnough(PropertyHelper.parseStringToProperty(cost))){
            player.getModule(PreReduceModule.class).preReduce(cost);
        }else{
            respMsg.setRet(GameErrorCode.E_BAG_ITEM_NO_ENOUGH);
            player.sendPacket(ClientProtocol.U_FIVEHERO_REVIVE_PATRONS,respMsg);
            return;
        }
//        player.sendChannelPacket(CrossProtocol.C2_CROSS_FIVE_HERO_REVIVE_PATRONS, reqMsg.toBuilder());
        int groupId = GameUnionActivityGroupMgr.getGroupId(reqMsg.getActivityId(),player.getUserId());
        GamePlayerMgr.sendUnionActivityGroupPacket(CrossProtocol.C2_CROSS_FIVE_HERO_REVIVE_PATRONS,
                reqMsg.toBuilder(),player.getUserId(),reqMsg.getActivityId(),groupId);
    }

    //添加加战力道具
    public void addItem(int goodsId, BigInteger num) {
        ActivityInfo activityInfo = FiveHeroMgr.getActivityInfo();
        if(activityInfo != null){
            FiveHeroProto.CrossFiveHeroAddItemMsg.Builder msg = FiveHeroProto.CrossFiveHeroAddItemMsg.newBuilder();
            msg.setActivityId(activityInfo.getActivityId());
            msg.setUserId(getUserId());
            msg.setItem(goodsId);
            msg.setNum(num.intValue());
            /*YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.C2_CROSS_FIVE_ABILITY_ADD_ITEM, msg);
            GamePlayerMgr.sendChannelPacket(player.getUserId(), pbMsg);*/
            int groupId = GameUnionActivityGroupMgr.getGroupId(activityInfo.getActivityId(),player.getUserId());
            GamePlayerMgr.sendUnionActivityGroupPacket(CrossProtocol.C2_CROSS_FIVE_ABILITY_ADD_ITEM,
                    msg,player.getUserId(),activityInfo.getActivityId(),groupId);
        }
    }
}
